<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;
use Carbon\Carbon;
use App\Models\DayOrderReport;

class OperationController extends Controller {
    /*
     * @@运营统计的首页
     */

    public function index(Request $request) {
        if (!$request->ajax()) {
            $startDt = Carbon::parse(Order::min('created_at')); //最早的一个订单时间
            $endDt = Carbon::parse(Order::max('created_at')); //最新的一个订单时间
            $yearStart = $startDt->year;
            $yearEnd = $endDt->year;
            while ($yearStart <= $yearEnd) {
                $yearSection[] = $yearStart;
                $yearStart++;
            }
            $persex = [
                'boy' => User::where('sex', 1)->count(),
                'girl' => User::where('sex', 0)->count()
            ];
            $persons = User::count();
            $age = [
                '15'=>User::where('age','<',15)->count(),
                '20'=>User::where('age','>=',15)->where('age','<',20)->count(),
                '30'=>User::where('age','>=',20)->where('age','<',30)->count(),
                '50'=>User::where('age','>=',30)->where('age','<',50)->count(),
                '51'=>User::where('age','>=',50)->count(),
            ];
            return view('admin/operation/operationlist', [
                'yearSection' => $yearSection,
                'persex' => $persex,
                'persons' => $persons,
                'age'=>$age
            ]);
        }
        $year = $request->input('year');
        $dt = Carbon::create($year, 1, 1, 0, 0, 0);
        switch ($request->input('type')) {
            //季度
            case 'quarter':
                $firstDay = $dt->toDateTimeString();
                $lastDay = $dt->addMonths(3)->subDay()->hour(23)->minute(59)->second(59)->toDateTimeString();
                $section = []; //各个时间区间
                //判断目前循环的订单时间是否小于等于最新订单的时间
                while ($dt->year == $year) {
                    $section[] = date('m-d', strtotime($firstDay)) . '~' . date('m-d', strtotime($lastDay));
                    //echo date('Y-m-d', strtotime($firstWeekDay)) . '~' . date('Y-m-d', strtotime($lastWeekDay)) . '<br>';
                    //营收额
                    $revenues = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->where('state', 1)
                            ->sum('total');
                    $amount = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('amount');
                    $sales = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('total');
                    $allOrdersRevenues[] = $revenues;
                    $allOrdersAmount[] = $amount;
                    $allOrdersSales[] = $sales;
                    $firstDay = $dt->addSecond()->toDateTimeString();
                    $lastDay = $dt->addMonths(3)->subDay()->hour(23)->minute(59)->second(59)->toDateTimeString();
                }
                break;
            //月
            case 'month':
                $firstDay = $dt->toDateTimeString();
                $lastDay = $dt->addMonth()->subDay()->hour(23)->minute(59)->second(59)->toDateTimeString();
                $section = []; //各个时间区间
                while ($dt->year == $year) {
                    $section[] = date('m-d', strtotime($firstDay)) . '~' . date('m-d', strtotime($lastDay));
                    //echo date('Y-m-d', strtotime($firstWeekDay)) . '~' . date('Y-m-d', strtotime($lastWeekDay)) . '<br>';
                    $revenues = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->where('state', 1)
                            ->sum('total');
                    $amount = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('amount');
                    $sales = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('total');
                    $allOrdersRevenues[] = $revenues;
                    $allOrdersAmount[] = $amount;
                    $allOrdersSales[] = $sales;
                    $firstDay = $dt->addDay()->hour(0)->minute(0)->second(0)->toDateTimeString();
                    $lastDay = $dt->addMonth()->subDay()->hour(23)->minute(59)->second(59)->toDateTimeString();
                }
                break;
            //周
            case 'week':
                $firstDay = $dt->toDateTimeString();
                $lastDay = $dt->addDays(6 - $dt->dayOfWeek)->hour(23)->minute(59)->second(59)->toDateTimeString();
                $section = []; //各个时间区间
                while ($dt->year == $year) {
                    $section[] = date('m-d', strtotime($firstDay)) . '~' . date('m-d', strtotime($lastDay));
                    //echo date('Y-m-d', strtotime($firstWeekDay)) . '~' . date('Y-m-d', strtotime($lastWeekDay)) . '<br>';
                    $revenues = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->where('state', 1)
                            ->sum('total');
                    $amount = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('amount');
                    $sales = Order::
                            where('created_at', '>=', $firstDay)
                            ->where('created_at', '<=', $lastDay)
                            ->sum('total');
                    $allOrdersRevenues[] = $revenues;
                    $allOrdersAmount[] = $amount;
                    $allOrdersSales[] = $sales;
                    $firstDay = $dt->addDay()->hour(0)->minute(0)->second(0)->toDateTimeString();
                    $lastDay = $dt->addDays(6)->hour(23)->minute(59)->second(59)->toDateTimeString();
                }
                $lastDay = $dt->day(1)->subDay()->toDateTimeString();
                $section[] = date('m-d', strtotime($firstDay)) . '~' . date('m-d', strtotime($lastDay));
                $revenues = Order::
                        where('created_at', '>=', $firstDay)
                        ->where('created_at', '<=', $lastDay)
                        ->where('state', 1)
                        ->sum('total');
                $amount = Order::
                        where('created_at', '>=', $firstDay)
                        ->where('created_at', '<=', $lastDay)
                        ->sum('amount');
                $sales = Order::
                        where('created_at', '>=', $firstDay)
                        ->where('created_at', '<=', $lastDay)
                        ->sum('total');
                $allOrdersRevenues[] = $revenues;
                $allOrderAmount[] = $amount;
                $allOrdersSales[] = $sales;
                break;
            //日
            case 'day':
                $section = []; //各个时间区间
                $allOrdersRevenues = [];
                $allOrdersAmount = [];
                $allOrdersSales = [];
                $reports = DayOrderReport::
                        where('order_date', '>=', $request->input('startDate'))
                        ->where('order_date', '<=', $request->input('endDate'))
                        ->get();
                foreach ($reports as $report) {
                    $section[] = $report->order_date;
                    $allOrdersRevenues[] = $report->pay_total;
                    $allOrdersAmount[] = $report->amount;
                    $allOrdersSales[] = $report->total;
                }
                break;
        }
        return [
            'section' => $section,
            'allOrdersRevenues' => $allOrdersRevenues,
            'allOrdersSales' => $allOrdersSales,
            'allOrdersAmount' => $allOrdersAmount
        ];
    }

    public function dayOrderReports(){
        
        
    }
    
}
